package com.bytedance.framwork.core.a;

import android.content.Context;
import android.text.TextUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedList;

public class c {
    public final class a implements Comparator {
        public a() {

        }

        public final int compare(Object arg5, Object arg6) {
            int v0 = ((File) arg5).lastModified() < ((File) arg6).lastModified() ? 1 : -1;
            return v0;
        }
    }

    Context a;
    Object b;
    public JSONObject c;
    static final FilenameFilter d;
    private volatile long e;
    private static volatile c f;
    private volatile String g;
    private volatile long h;
    private volatile boolean i;

    static {
        d = new d();
    }

    private c(Context arg5) {

        this.e = 204800;
        this.b = new Object();
        this.a = arg5;
        this.e = 204800;
    }

    private c(Context arg5, long arg6) {
        long v2 = 204800;

        this.e = v2;
        this.b = new Object();
        this.a = arg5;
        this.e = arg6 <= 0 || arg6 >= 10240 ? v2 : arg6 << 10;
    }

    public static c a() {
        return f;
    }

    final void a(String arg11) throws IOException {
        FileOutputStream v1_2 = null;
        File v4_1;
        String v0_3;
        File v0_2;
        File v1;
        int v0 = 0;
        String v2 = null;
        if (!TextUtils.isEmpty(((CharSequence) arg11))) {
            Object v3 = this.b;
            synchronized (v3) {
                try {
                    if (!this.i) {
                        this.i = true;
                        v1 = new File(android.support.design.a.i(this.a), "ss_monitor_trace_logs");
                        if (v1.exists()) {
                            File[] v1_1 = v1.listFiles(d);
                            if (v1_1 != null && v1_1.length != 0) {
                                Arrays.sort(v1_1, new a());
                                long v4 = System.currentTimeMillis();
                                while (v0 < v1_1.length) {
                                    if (v4 - v1_1[v1_1.length - 1 - v0].lastModified() <= 604800000) {
                                        break;
                                    }

                                    v1_1[v1_1.length - 1 - v0].delete();
                                    ++v0;
                                }
                            }
                        }
                    }

                    if (TextUtils.isEmpty(this.g)) {
                        v0_2 = new File(android.support.design.a.i(this.a), "ss_monitor_trace_logs");
                        if (!v0_2.exists()) {
                            v0_3 = v2;
                        } else {
                            File[] v0_4 = v0_2.listFiles(d);
                            if (v0_4 != null && v0_4.length != 0) {
                                Arrays.sort(((Object[]) v0_4), new a());
                                v0_3 = v0_4[0].getName();
                            } else {
                                v0_3 = v2;
                            }
                        }

                        this.g = v0_3;
                        this.h = this.b(this.g);
                    }

                    if (TextUtils.isEmpty(this.g)) {
                        this.g = "ss_monitor_trace-trace_file_1tt-trace.trace";
                        this.h = this.b(this.g);
                    }

                    v0_2 = new File(android.support.design.a.i(this.a), "ss_monitor_trace_logs");
                    if (!v0_2.exists()) {
                        v0_2.mkdirs();
                    }

                    v1 = new File(v0_2, this.g);
                    if (v1.length() > this.e) {
                        if (this.g.equalsIgnoreCase("ss_monitor_trace-trace_file_1tt-trace.trace")) {
                            this.g = "ss_monitor_trace-trace_file_2tt-trace.trace";
                        } else if (this.g.equalsIgnoreCase("ss_monitor_trace-trace_file_2tt-trace.trace")) {
                            this.g = "ss_monitor_trace-trace_file_3tt-trace.trace";
                        } else if (this.g.equalsIgnoreCase("ss_monitor_trace-trace_file_3tt-trace.trace")) {
                            this.g = "ss_monitor_trace-trace_file_4tt-trace.trace";
                        } else if (this.g.equalsIgnoreCase("ss_monitor_trace-trace_file_4tt-trace.trace")) {
                            this.g = "ss_monitor_trace-trace_file_5tt-trace.trace";
                        } else if (this.g.equalsIgnoreCase("ss_monitor_trace-trace_file_5tt-trace.trace")) {
                            this.g = "ss_monitor_trace-trace_file_6tt-trace.trace";
                        } else if (this.g.equalsIgnoreCase("ss_monitor_trace-trace_file_6tt-trace.trace")) {
                            this.g = "ss_monitor_trace-trace_file_7tt-trace.trace";
                        } else if (this.g.equalsIgnoreCase("ss_monitor_trace-trace_file_7tt-trace.trace")) {
                            this.g = "ss_monitor_trace-trace_file_8tt-trace.trace";
                        } else if (this.g.equalsIgnoreCase("ss_monitor_trace-trace_file_8tt-trace.trace")) {
                            this.g = "ss_monitor_trace-trace_file_9tt-trace.trace";
                        } else if (this.g.equalsIgnoreCase("ss_monitor_trace-trace_file_9tt-trace.trace")) {
                            this.g = "ss_monitor_trace-trace_file_10tt-trace.trace";
                        } else {
                            this.g = "ss_monitor_trace-trace_file_1tt-trace.trace";
                        }

                        this.h = ((long) arg11.length());
                        v4_1 = new File(v0_2, this.g);
                        if (v4_1.length() != 0) {
                            v1_2 = new FileOutputStream(v4_1);
                            ((OutputStream) v1_2).write("".getBytes());
                            a(v4_1, arg11);
                            return;
                        }

                        a(v4_1, arg11);
                        return;
                    } else {
                        this.h += ((long) arg11.length());
                        a(v1, arg11);
                        return;
                    }
                } catch (Throwable v0_1) {
                    v0_1.printStackTrace();
                } finally {
                    if (v1_2 != null) {
                        try {
                            v1_2.close();
                        } catch (Exception e) {

                        }
                    }
                }
            }
        }
    }

    final void a(File arg7) throws IOException {
        FileInputStream v1 = null;
        LinkedList v0_1;
        if (arg7 != null && (arg7.exists())) {
            FileInputStream v2 = null;
            try {
                v0_1 = new LinkedList();
                v1 = new FileInputStream(arg7);
                BufferedReader v3 = new BufferedReader(new InputStreamReader(((InputStream) v1)));
                while (true) {
                    String v2_1 = v3.readLine();
                    if (v2_1 == null) {
                        break;
                    }

                    if (v0_1.size() < 200) {
                        v0_1.add(v2_1);
                    } else {
                        this.a(v0_1);
                    }
                }
                this.a(v0_1);
            } catch (Throwable v0) {

            } finally {
                if (v1 != null) {
                    ((InputStream) v1).close();
                }

                if (arg7 != null && (arg7.exists())) {
                    arg7.delete();
                }
            }
        }
    }

    public static void a(Context arg2) {
        if (f == null) {
            Class v1 = c.class;
            synchronized (v1) {
                if (f == null) {
                    f = new c(arg2);
                    return;
                }
            }
        }
    }

    public static void a(Context arg3, long arg4) {
        if (f == null) {
            Class v1 = c.class;
            synchronized (v1) {
                if (f == null) {
                    f = new c(arg3, arg4);
                }
                return;
            }
        }
    }

    private static void a(File arg3, String arg4) throws IOException {
        FileOutputStream v1 = null;
        if (!TextUtils.isEmpty(((CharSequence) arg4))) {
            FileOutputStream v2 = null;
            try {
                v1 = new FileOutputStream(arg3, true);
                ((OutputStream) v1).write((arg4 + "\n").getBytes());
            } catch (Throwable v0) {
            } finally {
                try {
                    if (v1 != null) {
                        ((OutputStream) v1).close();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void a(LinkedList arg9) {
        try {
            JSONObject v1 = new JSONObject();
            JSONArray v2 = new JSONArray();
            JSONArray v3 = new JSONArray();
            JSONArray v4 = new JSONArray();
            JSONArray v5 = new JSONArray();
            label_10:
            while (!arg9.isEmpty()) {
                Object v0_1 = arg9.poll();
                String v6 = ((String) v0_1).substring(0, ((String) v0_1).indexOf(":"));
                String v0_2 = ((String) v0_1).substring(((String) v0_1).indexOf(":") + 1);
                try {
                    JSONObject v7 = new JSONObject(v0_2);
                    if (v6.equals("count")) {
                        v3.put(v7);
                        continue;
                    }

                    if (v6.equals("timer")) {
                        v4.put(v7);
                        continue;
                    }

                    if (v6.equals("store")) {
                        v5.put(v7);
                        continue;
                    }

                    v2.put(v7);
                    continue;
                } catch (Exception v0_3) {
                    v0_3.printStackTrace();
                    continue;
                }
            }

            if (v2.length() > 0) {
                v1.put("data", v2);
            }

            if (v3.length() > 0) {
                v1.put("count", v3);
            }

            if (v4.length() > 0) {
                v1.put("timer", v4);
            }

            if (v5.length() > 0) {
                v1.put("store", v5);
            }

            if (v1.length() <= 0) {
                return;
            }

            if (this.c != null) {
                JSONObject v2_1 = this.c;
                v1.put("header", v2_1);
            }

            android.support.design.a.l(v1.toString());
            return;
        } catch (Throwable v0) {
        }
    }

    private long b(String arg5) {
        long v0;
        if (TextUtils.isEmpty(((CharSequence) arg5))) {
            v0 = 0;
        } else {
            File v0_1 = new File(android.support.design.a.i(this.a), "ss_monitor_trace_logs");
            if (!v0_1.exists()) {
                v0_1.mkdirs();
            }
            v0 = new File(v0_1, "ss_monitor_trace-" + arg5).length();
        }

        return v0;
    }
}

